package com.easyplan.planningresultreciver.controller;

import com.alibaba.fastjson.JSON;
import com.easyplan.planningresultreciver.domain.PlannedResult;


import java.io.*;

/**
 * 结果处理工具类
 */
public class ResultService {

    /**
     * 将结果保存为JSON文件
     * @param plannedResult
     * @throws IOException
     */
    public static String saveResult(PlannedResult plannedResult) throws IOException {

        String resultString = JSON.toJSONString(plannedResult, true);

        String targetFileName = System.getProperty("user.dir") + File.separator + "data" + File.separator + "Result-" + plannedResult.getPlanningId() + ".json";

        File file = new File(targetFileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        write.write(resultString);
        write.flush();
        write.close();

        return targetFileName;
    }

}
